package com.rla.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.rla.common.core.page.TableDataInfo;
import com.rla.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rla.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.rla.domain.bo.MallOrderDetailBo;
import com.rla.domain.vo.MallOrderDetailVo;
import com.rla.domain.MallOrderDetail;
import com.rla.mapper.MallOrderDetailMapper;
import com.rla.service.IMallOrderDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 订单明细商品Service业务层处理
 *
 * @author rla
 * @date 2025-08-08
 */
@RequiredArgsConstructor
@Service
public class MallOrderDetailServiceImpl implements IMallOrderDetailService {

    private final MallOrderDetailMapper baseMapper;

    /**
     * 查询订单明细商品
     */
    @Override
    public MallOrderDetailVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询订单明细商品列表
     */
    @Override
    public TableDataInfo<MallOrderDetailVo> queryPageList(MallOrderDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MallOrderDetail> lqw = buildQueryWrapper(bo);
        Page<MallOrderDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询订单明细商品列表
     */
    @Override
    public List<MallOrderDetailVo> queryList(MallOrderDetailBo bo) {
        LambdaQueryWrapper<MallOrderDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MallOrderDetail> buildQueryWrapper(MallOrderDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MallOrderDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getOrderId()), MallOrderDetail::getOrderId, bo.getOrderId());
        lqw.eq(StringUtils.isNotBlank(bo.getDrugCode()), MallOrderDetail::getDrugCode, bo.getDrugCode());
        lqw.eq(StringUtils.isNotBlank(bo.getSkuCode()), MallOrderDetail::getSkuCode, bo.getSkuCode());
        lqw.eq(bo.getPrice() != null, MallOrderDetail::getPrice, bo.getPrice());
        lqw.eq(bo.getNum() != null, MallOrderDetail::getNum, bo.getNum());
        lqw.eq(StringUtils.isNotBlank(bo.getUnit()), MallOrderDetail::getUnit, bo.getUnit());
        lqw.eq(bo.getAmount() != null, MallOrderDetail::getAmount, bo.getAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getBatchNum()), MallOrderDetail::getBatchNum, bo.getBatchNum());
        lqw.eq(bo.getSort() != null, MallOrderDetail::getSort, bo.getSort());
        lqw.eq(bo.getUsePoints() != null, MallOrderDetail::getUsePoints, bo.getUsePoints());
        lqw.like(StringUtils.isNotBlank(bo.getErpDrugName()), MallOrderDetail::getErpDrugName, bo.getErpDrugName());
        lqw.like(StringUtils.isNotBlank(bo.getErpSpeciName()), MallOrderDetail::getErpSpeciName, bo.getErpSpeciName());
        lqw.eq(StringUtils.isNotBlank(bo.getErpSpeciCode()), MallOrderDetail::getErpSpeciCode, bo.getErpSpeciCode());
        return lqw;
    }

    /**
     * 新增订单明细商品
     */
    @Override
    public Boolean insertByBo(MallOrderDetailBo bo) {
        MallOrderDetail add = BeanUtil.toBean(bo, MallOrderDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改订单明细商品
     */
    @Override
    public Boolean updateByBo(MallOrderDetailBo bo) {
        MallOrderDetail update = BeanUtil.toBean(bo, MallOrderDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MallOrderDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除订单明细商品
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
